<?php

/**
 * @file SendEmail
 * @author garypradoruiz <garypradoruiz@gmail.com>
 * @link mailto:garypradoruiz@gmail.com
 * @copyleft 2012 garypradoruiz
 * @license GPL
 * @version 1
 */
class Amz_Resource_SendEmailTask {

    public function sendEmailTask($id_orden = 0, $id_tarea = 0, $type = '') {
        // list emails for send
        $toMails = $this->getEmailOrderTask($id_tarea);
        $orden = $this->getDataOrdenId($id_orden);
        $tarea = $this->getDataTarea($id_tarea);

        if ($orden != null) {
            $smtpServer = 'mail.velcro-lab.com';
            $username = 'admpro@velcro-lab.com';
            $password = 'admpro';


            $config = array('ssl' => 'tls',
                'auth' => 'login',
                'username' => $username,
                'password' => $password);

            $transport = new Zend_Mail_Transport_Smtp($smtpServer, $config);
            
//            $config = array('smtp' => 'mail.velcro-lab.com');
//            $transport = new Zend_Mail_Transport_Smtp('mail.velcro-lab.com', $config);
//
//            // Set From & Reply-To address and name for all emails to send.
            Zend_Mail::setDefaultFrom('admpro@velcro-lab.com', 'Seguimiento Proyectos');
            // create view object
            $html = new Zend_View();
            $html->setScriptPath(APPLICATION_PATH . '/modules/orden/views/emails/');
            $html->assign('orden', $orden);
            $html->assign('tarea', $tarea);
            $html->assign('users', $toMails);

            $request = Zend_Controller_Front::getInstance()->getRequest();
            //$url = $request->getScheme() . '://' . $request->getHttpHost() . '';
            $url = $request->getScheme() . '://' . $request->getHttpHost() . '/capataz';
            // Loop through messages
            foreach ($toMails as $to) {
                $html->assign('name', $to['name']);

                switch ($type) {
                    case 'ADD_TASK':
                        $html->assign('link', $url . '/tarea/index/viewtarea/id/' . $id_tarea);
                        $bodyText = $html->render('new-task.phtml');
                        $subject = 'Nueva Tarea';
                        break;
                    case 'EDIT_TASK':
                        $html->assign('link', $url . '/orden/orden/viewtarea/id/' . $id_tarea);
                        $bodyText = $html->render('new-task.phtml');
                        $subject = 'Tarea Antigua Modificada';
                        break;
                }

                // Email config send
                $mail = new Zend_Mail();
                $mail->addTo($to['email'], $to['name']);
                $mail->setSubject($subject);
                $mail->setBodyHtml($bodyText);
                $mail->send($transport);
            }

            // Reset defaults
            Zend_Mail::clearDefaultFrom();
        }
    }

    private function getEmailOrderTask($id_tarea = 0) {

        $tareaDb = new Tarea_Model_DbTable_Tarea();
        $tareaUserDb = new Tarea_Model_DbTable_TareaUser();

        $toMails = array();
        //get user create task
        $selectU = $tareaUserDb->select()->setIntegrityCheck(false)
                ->from(array('t' => 'tarea'), array('*'))
                ->join(array('u' => 'user'), 't.id_user_create = u.id and t.id=' . $id_tarea);
        $user_create = $tareaDb->fetchRow($selectU);

        $names = $user_create->nombre . ' ' . $user_create->apellidos;
        $toMails[] = array('name' => $names, 'email' => $user_create->email);

        // get user tarea...
        $select = $tareaUserDb->select()->setIntegrityCheck(false)
                ->from(array('t' => 'tarea_user'), array('*'))
                ->join(array('u' => 'user'), 't.id_tarea = ' . $id_tarea . ' and t.id_user=u.id');
        $resultUserAll = $tareaUserDb->fetchAll($select);
        foreach ($resultUserAll as $user) {
            $names = $user->nombre . ' ' . $user->apellidos;
            $toMails[] = array('name' => $names, 'email' => $user->email);
        }

        //add email statics, include the extract emails from databases   
        //$toMails[] = array('name' => 'Gary Prado', 'email' => 'garypradoruiz@gmail.com');

        return $toMails;
    }

    /*
     * Get data orden Id only a row
     */

    private function getDataOrdenId($id_orden = 0) {
        $ordenDb = new Orden_Model_DbTable_Orden();

        $select = $ordenDb->select()->setIntegrityCheck(false)
                ->from(array('o' => 'orden'), array('id',
                    'code',
                    'title',
                    'description',
                    'create_date',
                    'end_date',
                    'status',
                    'deny_description')
                )->where('o.id=?', $id_orden)
                ->join(array('ou' => 'orden_user'), 'o.id = ou.id_orden')
                ->join(array('u' => 'user'), 'u.id = ou.id_user')
                ->join(array('cl' => 'client'), 'cl.id = u.client_departament', array('name'));

        $row = $ordenDb->fetchRow($select);
        if ($row != null)
            return $row;
        else
            return null;
    }

    private function getDataTarea($id_tarea = 0) {
        if ($id_tarea != 0) {
            $tareaDb = new Tarea_Model_DbTable_Tarea();
            $tarea = $tareaDb->fetchRow('id=' . $id_tarea);

            return $tarea;
        }
    }

}

?>
